/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "EP3DEnumAttribute.h"
namespace EP3D
{
	EnumAttribute::EnumAttribute(const TCHAR* name, const TCHAR* value, const TCHAR* const* literals)
	{
		m_kName= name;
		SetEnum(value, literals);
	}

	int EnumAttribute::GetInt() const
	{
		for (uint i = 0; m_akEnumLiterals.GetSize(); ++i) {
			if (m_kVal.CompareNoCase(m_akEnumLiterals[i])) {
				return (int)i;
			}
		}

		return -1;
	}

	void EnumAttribute::SetInt(int val)
	{
		if (val >= 0 && val < (int)m_akEnumLiterals.GetSize()) {
			m_kVal = m_akEnumLiterals[(uint)val];
		}
		else {
			m_kVal = TEXT("");
		}
	}

	f32 EnumAttribute::GetF32() const
	{
		return (f32)GetInt();
	}

	void EnumAttribute::SetF32(f32 val)
	{
		SetInt((int)val);
	}

	f64 EnumAttribute::GetF64() const
	{
		return (f64)GetInt();
	}

	void EnumAttribute::SetF64(f64 val)
	{
		SetInt((int)val);
	}

	bool EnumAttribute::GetBool() const
	{
		return (GetInt() != 0);
	}

	String EnumAttribute::GetString() const
	{
		return m_kVal;
	}

	void EnumAttribute::SetString(const String& val)
	{
		m_kVal = val;
	}

	void EnumAttribute::SetString(const TCHAR* val)
	{
		m_kVal = val;
	}

	const TCHAR* EnumAttribute::GetEnum() const
	{
		return m_kVal;
	}

	void EnumAttribute::SetEnum(const TCHAR* val, const TCHAR* const* literals)
	{
		int nLiteralCount = 0;

		if (literals) {
			int i;
			for (i = 0; literals[i]; ++i){
				nLiteralCount++;
			}

			for (i = 0; literals[i]; ++i) {
				m_akEnumLiterals.Add(literals[i]);
			}
		}

		SetString(val);
	}

	AttributeType EnumAttribute::GetType() const
	{
		return AT_ENUM;
	}
	
	const TCHAR* EnumAttribute::GetTypeString() const
	{
		return TEXT("enum");
	}
}
